package objbo.xmlparser.model.modeler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import objbo.xmlparser.model.bean.XMLFileBean;

public class XMLFileModeler
{
	private XMLFileBean fileBean = null;
	//private URL url = null;
	
	public XMLFileModeler()
	{
		this.fileBean = new XMLFileBean();
	}
	
	public boolean isLoaded()
	{
		boolean isThat = false;
		if(this.fileBean != null)
		{
			isThat = true;
		}
		return isThat;
	}

	public XMLFileBean getFileBean()
	{
		return this.fileBean;
	}

	public void setFileBean(XMLFileBean fileBean)
	{
		this.fileBean = fileBean;
	}
	
	private void setFileName(String fileName)
	{
		URI uri = null;
		try
		{
			uri = new File( fileName ).toURI();
			this.fileBean.setFileURI(uri);
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	private void setFileURI(URI uri)
	{

		this.fileBean.setFileURI(uri);

	}
	
	private void setFileURL(URL url)
	{
			
		try
		{
			this.fileBean.setFileURI(url.toURI());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	private void setFile(Object file)
	{
		if(file instanceof String)
		{
			this.setFileName((String) file);
		}
		else if (file instanceof URI)
		{
			setFileURI((URI) file);
		}
		else if (file instanceof URL)
		{
			setFileURL((URL) file);
		}
	}
		
	private boolean loadFile()
	{
		boolean isDone = false;
		
		SAXReader reader = null;
		Document document = null;

		try
		{
			reader = new SAXReader();
			if(this.fileBean.getFileURI() != null)
			{
				document = reader.read(this.fileBean.getFileURI().toURL());
				if(document != null)
				{
					this.fileBean.setDocument(document);
					isDone = true;
				}
				
			}
		
			
			
		}
		catch (Exception e)
		{
			isDone = false;
			e.printStackTrace();
		}
		
		return isDone;
	}
	
	public boolean loadFile(Object file)
	{
		if(file != null)
		{
			setFile(file);
		}
		return loadFile();
	}
	
	
	
	
	
	private boolean saveFile()
	{
		boolean isDone = false;
		
		XMLWriter writer = null;
		FileWriter fileWriter = null;
		
		
		
		
		try
		{
			if(this.fileBean.getFileURI() != null && this.fileBean.getDocument() != null)
			{
				fileWriter = new FileWriter(new File(this.fileBean.getFileURI()));
				writer = new XMLWriter(fileWriter);
				
				writer.write(this.fileBean.getDocument() );
				
				isDone= true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				writer.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		
		return isDone;
	}
	
	public boolean saveFile(Object fileName)
	{
		this.setFile(fileName);
		return saveFile();
		
	}
	
	
}
